<?php

namespace App\Http\Controllers;

use App\Lib\Api;
use App\Lib\CacheKey;
use App\Service\LogService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;

use App\Service\TradeService;

class TradeController extends Controller
{
    /**
     * TradeController constructor.
     */
	public function __construct()
	{
        parent::__construct();
	}

    //检测交易链接
    public function checkTradeUrl(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'tradeUrl' => 'required',
            'steamId' => 'required',
        ]);

        if ($validator->fails()) {
            return Api::error(Api::CODE_PARAM_ERROR);
        }

        $tradeUrl = $request->input('tradeUrl');
        $steamId = $request->input('steamId');
        $botId = $request->input('botId', 0);

        list($code, $result) = TradeService::getInstance()->checkTradeUrl($tradeUrl, $steamId, $botId);

        return ($code == Api::CODE_OK) ? Api::success($result) : Api::error($code);
    }

    //发起报价
    public function from(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'app_id' => 'required',
            'steam_id' => 'required',
            'tradeUrl' => 'required',
            'o_ids' => 'required',
        ]);

        if ($validator->fails()) {
            return Api::error(Api::CODE_PARAM_ERROR);
        }

        $appId = $request->input('app_id');
        $steamId = $request->input('steam_id');
        $tradeUrl = $request->input('tradeUrl');
        $o_ids = $request->input('o_ids');
        $o_ids = explode(',', $o_ids);

        //判断用户存入不能超过500
        $count = count($o_ids);
        if ($count > 500) {
            return Api::error(Api::CODE_FROM_TRADE_ITEM_COUNT);
        }

        //检测用户是否有报价
        $trades = TradeService::getInstance()->checkCreatedDoing($steamId);
        if ($trades > 0) {
            return Api::error(Api::CODE_FROM_TRADE_COUNT);
        }

        //可以发起交易报价了
        $createOrderStatusKey = sprintf(CacheKey::CREATE_ORDER_STATUS_STEAMID, $steamId);
        $createOrderStatus = Cache::store('redis')->get($createOrderStatusKey);

        if ($createOrderStatus == 1) {    //请勿重复操作
            return Api::error(Api::CODE_REPEAT_SUBMIT_ERROR);
        }
        Cache::store('redis')->put($createOrderStatusKey, 1, 2);
        list($code, $result) = TradeService::getInstance()->createFrom($steamId, $tradeUrl, $o_ids, $appId);
        //可以发起交易报价了
        Cache::store('redis')->put($createOrderStatusKey, 0, 5);

        if ($code != Api::CODE_OK) {
            LogService::getInstance()->addActionLog(__FUNCTION__, 0, $steamId, Api::$messages[$code]);
            return Api::error($code);

        }
        return Api::success($result);
    }

    /**
     * 取回报价api
     */
    public function out(Request $request)
    {
        $steamId  = $request->input('steam_id', '');
        $tradeUrl = $request->input('tradeUrl', '');
        $list = $request->input('list', '');
        $list = $data = json_decode($list, true);

        if(!$list){
            return Api::error(Api::CODE_PARAM_ERROR);
        }

        list($code, $result) = TradeService::getInstance()->createOut($steamId, $tradeUrl, $list);
        if ($code != Api::CODE_OK) {
            LogService::getInstance()->addActionLog(__FUNCTION__, 0, $steamId, json_encode($result));
            return Api::error($code);
        }
        return Api::success($result);
    }

    /**
     * 获取存入报价状态
     */
    public function checkFromTradeStatus(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'from_trade_id' => 'required',
        ]);
        if ($validator->fails()) {
            return Api::error(Api::CODE_PARAM_ERROR);
        }
        $fromTradeId = $request->input('from_trade_id');
        $result = TradeService::getInstance()->getFromTradeInfo($fromTradeId);
        if(!$result){
            return Api::error(Api::CODE_NOT_THIS_TRADE);
        }
        return Api::success($result);
    }

    /**
     * 获取取回报价状态
     */
    public function checkOutTradeStatus(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'outTradeId' => 'required',
        ]);

        if ($validator->fails()) {
            return Api::error(Api::CODE_PARAM_ERROR);
        }

        $outTradeId = $request->input('outTradeId');

        $result = TradeService::getInstance()->getOutTradeInfo($outTradeId);
        if(!$result){
            return Api::error(Api::CODE_NOT_THIS_TRADE);
        }
        return Api::success($result);
    }
}
